﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Windows;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Database;
using ReflectionStudio.Core.Events;
using SDTVKVV;
using INTEX.DataBase.Tools;
namespace WPFExcelReport
{
    public partial class SDTVKVVData
    {
        public string ApplicationPath
        {
            get { return System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath); }
        } 
        /// <summary>
        /// GetJob is used to collect the tasks to finish the order creation
        /// </summary>
        /// <param name="FInterID"></param>
        /// <returns></returns>
        public DataSet GetJob()
        {

            ArrayList tbs = new ArrayList() { "A", "B", "C" };
            string a = (string)tbs[0];
            DataSet Result = new DataSet();
            DataSet Return = new DataSet();

            try
            {
                string fileDone;
                string filestemp = @"C:\IntexReport\Data\SDTVKVV\01Temp";
                string[] files = System.IO.Directory.GetFiles(filestemp);
                foreach (string file in files)
                {
                    fileDone = file.Replace(@"\01Temp\", @"\02Incoming\");
                    System.IO.File.Move(file, fileDone);
                }
                string files2p = @"C:\IntexReport\Data\SDTVKVV\02Incoming";
                files = System.IO.Directory.GetFiles(files2p);

                if (files.Length > 0)
                {
                    string file = files[0];
                    Result.ReadXml(file);
                    Return.Merge(Result);
                    fileDone = file.Replace(@"\02Incoming\", @"\03Done\");
                    System.IO.File.Move(file, fileDone);

                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {

            }
            return Return;
        }
        public void SaveJob(DataSet SetOrder)
        {
            ArrayList tbs = new ArrayList() { "A", "B", "C" };
            string a = (string)tbs[0];
            DataSet Result = new DataSet();
            DataSet Return = new DataSet();
            string fileroot = @"C:\IntexReport\Data\SDTVKVV\01Temp\" + "VVVK" + DateTime.Now.ToFileTime().ToString() + ".XML";
            SetOrder.WriteXml(fileroot);
        }
        private DataSet ConsolidateData(DataSet src)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                ReflectionStudio.Controls.PreisConverter pconverter = new ReflectionStudio.Controls.PreisConverter();
                ReflectionStudio.Controls.Divideby100 d100 = new ReflectionStudio.Controls.Divideby100();
                if(src.Tables.Contains("VKPOS_EXT"))
                {
                    if (src.Tables["VKPOS_EXT"].Rows.Count > 0)
                    {
                        foreach (DataRow row in src.Tables["VKPOS_EXT"].Rows)
                        {
                            row["VKPMANDANT"] = Left(row["VKPMANDANT"].ToString() + "                  ", 2);
                            row["VKPAUFKRS"] = Left(row["VKPAUFKRS"].ToString() + "                  ", 2);
                            row["VKPARTST"] = Left(row["VKPARTST"].ToString() + "                  ", 1);
                            
                            row["VKPKOLL"] = Left(row["VKPKOLL"].ToString() + "                  ", 5);
                            row["VKPART"]=Left(row["VKPART"].ToString()+"                  ",24);
                            row["VKPFARBE"] = Left(row["VKPFARBE"].ToString() + "                  ", 10);
                            row["VKPMENGE1"] = System.Convert.ToDouble(row["VKPMENGE1"].ToString()) * 100;
                            row["VKPMEEH"] = Left(row["VKPMEEH"].ToString() + "                  ", 3);
                            row["VKPPREIS"] = pconverter.ConvertBack(row["VKPPREIS"].ToString(),null,null,null);   
                            row["VKPLIEFTERM"] = Left(row["VKPLIEFTERM"].ToString() + "                  ", 8);
                            row["VKPPREH"] = Left(row["VKPPREH"].ToString() + "                  ", 3);
                            row["VKPNEUBEN"] = Left(row["VKPNEUBEN"].ToString() + "                  ", 8);
                            row["VKPNEUDAT"] = Left(row["VKPNEUDAT"].ToString() + "                  ", 8);
                            row["VKPPOSWERT"] = row["VKPPOSWERT"];
                        }
                    }
                }
                setReturn.Merge(src);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet LoadExcelVKVV()
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                #region=======MAPINTEX================
                string fileroot = @"C:\IntexReport\Data\SDTVKVV\EXCEL";
                string[] files = System.IO.Directory.GetFiles(fileroot);
                foreach (string file in files)
                {
                    string ShortName = file.Substring(file.LastIndexOf("\\") + 1);
                    EventDispatcher.Instance.RaiseStatus("装载:"+ShortName+"开始", StatusEventType.StopProgress);
                    if (!ShortName.StartsWith("VKVV"))
                        continue;
                    if (exAgent == null)
                    {
                        exAgent = new ExcelX.ExcelLib.ExcelAgent(file);
                    }
                    if (System.IO.File.Exists(file))
                    {
                        setResult = new DataSet();
                        setResult = exAgent.LoadDataTableDef(file, "");
                        setReturn.Merge(setResult);
                        string xml = @"C:\IntexReport\Data\SDTVKVV\01Temp\VVVK" + DateTime.Now.ToFileTime().ToString() + ".XML";
                        if (System.IO.File.Exists(xml))
                        {
                            System.IO.File.Delete(xml);
                        }
                        if (setResult != null)
                        {
                            setResult=ConsolidateData(setResult); 
                            setResult.WriteXml(xml);
                        }                        
                        System.IO.File.Copy(file, fileroot + "\\P_" + ShortName);
                        exAgent.Close();
                        System.IO.File.Delete(file);
                        EventDispatcher.Instance.RaiseStatus("装载:" + ShortName + "完成", StatusEventType.StopProgress);
                    }                
                }
                #endregion
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                if (exAgent != null)
                {
                    exAgent.KillExcelProcess();
                    exAgent.Dispose();
                    GC.Collect();
                    exAgent.Close();
                }
            }
            return setReturn;
        }

    }
}
